//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ioc\register.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);

private ["_namespace","_index","_pveh","_local"];
DEFAULT_PARAM(_namespace,0,"");
DEFAULT_PARAM(_local,1,false);
if (isNil QGVAR(INITIALIZED)) then {
	if (isNil QGVAR(QueuedRegistrations)) then {
		GVAR(QueuedRegistrations) = [];
	};
	TLOG_1("Queued registration",_this);
	GVAR(QueuedRegistrations) set [count GVAR(QueuedRegistrations), _this];
} else {
	if (IS_STRING(_namespace) && {_namespace != ""}) then {
		_namespace = toLower(_namespace);
		TRACE_1("Checking namespace",ARR_2(_namespace, count GVAR(NAMESPACES)));
		if (_namespace in GVAR(NAMESPACES)) then {
			TLOG_1("Namespace Already Registered",_namespace);
		} else {
			_index = count GVAR(NAMESPACES);
			GVAR(NAMESPACES) set [_index, _namespace];
			GVAR(EVENTS) set [_index, []];
			GVAR(HANDLERS) set [_index, []];
			_pveh = _namespace call FUNC(getPveh);
			GVAR(NAMESPACE_PVEH) set [_index, _pveh];
			if (!_local) then {
				_pveh  addPublicVariableEventHandler FUNC(pveh);
			};
			TRACE_1("Registered NameSpace",ARR_3(_namespace,_index,_pveh));
		};
	} else {
		ERROR_1("Invalid Input",_this);
	};
};
TRACE("End");